﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using KTXD.Entities;
using KTXD.Data;
using System.Data;

namespace UserKTXD.DCTEDU_.CauHinhWeb
{
    public partial class QLCauHinhHomeControlCapHai : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindCatalog.BindSubChuyenMuc(cboSubChuyenMuc);
                Pager1.CurrentIndex = 1;
                Bind_Data(Pager1.CurrentIndex);
            }
        }

        protected void Bind_Data(int PageNo)
        {
            int TotalRows = 0;
            string WhereClause = "1=1";
            DataSet dsHomeControl = DataRepository.Provider.ExecuteDataSet("HomeControlCapHai_GetPaged", WhereClause, "Id DESC", PageNo - 1, Pager1.PageSize);
            gridCatalog.DataSource = dsHomeControl.Tables[0];
            gridCatalog.DataBind();
            try
            {
                TotalRows = Convert.ToInt32(dsHomeControl.Tables[1].Rows[0][0]);
            }
            catch (Exception)
            {
            }
            Pager1.ItemCount = TotalRows;
            if (TotalRows <= Pager1.PageSize)
                Pager1.Visible = false;
            else
                Pager1.Visible = true;
        }

        protected void Pager1_Command(object sender, CommandEventArgs e)
        {
            try
            {
                int currnetPageIndex = Convert.ToInt32(e.CommandArgument);
                Pager1.CurrentIndex = currnetPageIndex;
                Bind_Data(Pager1.CurrentIndex);
            }
            catch (Exception ex)
            {
                Common.WriteLog("QLCauHinhHomeControlCapHai.aspx.cs", "Pager1_Command()", ex.ToString());
            }
        }

        protected void cmdSave_Click(object sender, ImageClickEventArgs e)
        {
            bool updateStatus = false;
            try
            {//oo
                HomeControlCapHai entHomeControlCapHai = new HomeControlCapHai();
                entHomeControlCapHai.Id = Convert.ToInt32(H_CatalogId.Value);
                entHomeControlCapHai.IdSubChuyenMuc = Convert.ToInt32(cboSubChuyenMuc.SelectedValue);
                entHomeControlCapHai.ChuThich = Common.StandardString(txtDuongDan.Text);
                if (entHomeControlCapHai.Id == 0)
                {
                    entHomeControlCapHai.Id = 0;
                    updateStatus = DataRepository.HomeControlCapHaiProvider.Insert(entHomeControlCapHai);
                }
                else
                {
                    updateStatus = DataRepository.HomeControlCapHaiProvider.Update(entHomeControlCapHai);
                }
                if (updateStatus)
                {
                    ScriptManager.RegisterStartupScript(UpdatePanel1, this.GetType(), Constants.THATBAI, Constants.HOMECONTROL_CAPNHAT_THANHCONG, true);
                    Bind_Data(Pager1.CurrentIndex);
                    cmdReset_Click(sender, e);
                }
                else
                    ScriptManager.RegisterStartupScript(UpdatePanel1, this.GetType(), Constants.THATBAI, Constants.HOMECONTROL_CAPNHAT_THATBAI, true);
            }
            catch (Exception ex)
            {
                Common.WriteLog("QLCauHinhHomeControlCapHai.aspx.cs", "cmdSave_Click", ex.ToString());
                ScriptManager.RegisterStartupScript(UpdatePanel1, this.GetType(), Constants.THATBAI, Constants.HOMECONTROL_CAPNHAT_THATBAI, true);
            }
        }

        protected void cmdReset_Click(object sender, ImageClickEventArgs e)
        {
            cboSubChuyenMuc.SelectedValue = "0";
            txtDuongDan.Text = "";
            H_CatalogId.Value = "0";
        }

        protected void gridCatalog_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                e.Row.Attributes["onmouseover"] = "this.style.cursor='hand';this.style.backgroundColor='WhiteSmoke';";
                e.Row.Attributes["onmouseout"] = "this.style.backgroundColor='White';";
            }
        }

        protected void gridCatalog_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "Select")
            {
                try
                {
                    H_CatalogId.Value = e.CommandArgument.ToString();
                    HomeControlCapHai entHomeControlCapHai = DataRepository.HomeControlCapHaiProvider.GetById(Convert.ToInt32(H_CatalogId.Value));
                    if (entHomeControlCapHai != null)
                    {
                        txtDuongDan.Text = entHomeControlCapHai.ChuThich;
                        cboSubChuyenMuc.SelectedValue = entHomeControlCapHai.IdSubChuyenMuc.ToString();
                    }
                }
                catch (Exception ex)
                {
                    Common.WriteLog("QLCauHinhHomeControlCapHai.aspx.cs", "gridCatalog_RowCommand - Select", ex.ToString());
                }
            }
        }

        protected void gridCatalog_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            Bind_Data(Pager1.CurrentIndex);
            cboSubChuyenMuc.SelectedValue = "0";
            txtDuongDan.Text = "";
            H_CatalogId.Value = "0";
        }

        public string GetTenSubChuyenMuc(object Char)
        {
            int idMenu = Convert.ToInt32(Char.ToString());
            using (SubChuyenMuc entLoaiChuyenMuc = DataRepository.SubChuyenMucProvider.GetById(idMenu))
            {
                return entLoaiChuyenMuc.TenSubChuyenMuc;
            }
        }
    }
}